package testgrails

import java.security.Timestamp

/*
A Listing is an item for sale at AuctionHaus. Listings have the following requirements:
L-1: Listings have the following required fields: name, end date/time, and starting bid price (unit test)
L-2: Listings have the following optional fields: description (unit test)
L-3: Listings are required to have a seller (Customer) (unit test)
L-4: Listing descriptions must be less than 256 characters (unit test)
L-5: Listing end date/time must be in the future (unit test)
L-6: Listing name must be less than 64 characters (unit test)
L-7: Listing has a nullable field for the winner (Customer) (unit test)
*/
class Bid {
	double amount
	double currentAmount
	Date bidDate
	Customer bidder
	
	Listing listing 
	static belongsTo = [listing : Listing]
	
    static constraints = {
		currentAmount(nullable: false)
		amount validator: {
		amount, Bid -> if( Bid.currentAmount != null ) return amount >= (Bid.currentAmount + 0.50)				
		}
    }
    
    /** 
       MC -- I'm confused by the need for a currentAmount.  Is it only for comparison purposes (So the .50 increment works)?  It seems to break encapsulation.
       The way I did it was to actually iterate through all of the other bids to find the "current" amount.  (Not sure if that is the best way to do it either, though)
       
       Also -- the "Listing listing" declaration is redundant, Grails picks it up just by having this:  	static hasOne = [listing : Listing]  (Not sure if it should be hasOne (I have) or belongsTo (you have) though)
       And I think you need to actually declare the bidder relationship as a "belongsTo" like this so that Grails "knows" about the relationship:  	static belongsTo = [bidder:Customer]
       
       I also named my Date as dateCreated so that Grails will automatically fill it  ( http://grails.org/doc/2.0.x/ref/Database%20Mapping/autoTimestamp.html )
    */
}
